const Router = require('koa-router')
const { ArticleController, AuthcodeController } = require('../../controllers')
const { HttpDataResolve, Validator } = require('../../common')
const { Auth, SP_ADMIN, ADMIN } = require('../../middleware/auth')
const res = new HttpDataResolve()

const router = new Router({
  prefix: '/api/v1/article'
})

router.post('/', new Auth().m, async ctx => {
  const data = new Validator(ctx, {
    title: 'required|max:50|min:1',
    content: 'required',
    html: 'required',
    category_id: 'required|number',
    cover: '',
    status: 'required|include:0,1',
    label: 'max:128'
  })

  const article = await ArticleController.create(data, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.json({
    id: article.id
  }, '创建文章成功');
})

router.get('/:id/detail', new Auth().l, async ctx => {
  const data = new Validator(
    ctx.params,
    {
      id: 'required|number'
    },
    true
  )  
  const data2 = new Validator(
    ctx.query,
    {
      edit: 'include:true'
    },
    true
  )

  const article = await ArticleController.getArticle(data, data2, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.json(article);
})

router.delete('/', new Auth().m,  async ctx => {
  const data = new Validator(ctx, {
    id: 'required|number'
  })

  await ArticleController.delete(data, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.success();
})

router.post('/update', new Auth().m, async ctx => {
  const data = new Validator(ctx, {
    id: 'required|number',
    title: 'min:1|max:50',
    content: '',
    html: '',
    category_id: 'number',
    label: '',
    cover: ''
  })

  await ArticleController.update(data, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.success();
})

router.get('/list', new Auth().l, async ctx => {
  const data = new Validator(ctx.query, {
    page: 'number',
    size: 'number',
    keyword: '',
    user_id: 'number',
    order: 'include:DESC,ASC',
    label: '',
    status: 'number',
    getCache: 'include:false'
  }, true)
  const result = await ArticleController.list(data, false, ctx.auth);

  ctx.response.status = 200;
  ctx.body = res.json(result)
})

router.post('/disable', new Auth(ADMIN).m, async ctx => {
  const data = new Validator(ctx, {
    id: 'required|number',
    info: 'required'
  })

  const flag = await ArticleController.disable(data, ctx);
  if(!flag) return;

  ctx.response.status = 200;
  ctx.body = res.success();
})

router.post('/request/unlock', new Auth().m, async ctx => {
  const data = new Validator(ctx, {
    id: "required|number"
  })

  const flag = await ArticleController.requestUnlock(data, ctx);
  if(!flag) return;

  ctx.response.status = 200
  ctx.body = res.success('请求成功，请耐心等待管理员处理');
})

router.get('/disable/list', new Auth(ADMIN).m, async ctx => {
  const data = new Validator(ctx.query, {
    page: 'number',
    size: 'number',
    keyword: '',
    user_id: 'number',
    order: 'include:DESC,ASC',
    label: ''
  }, true)
  const result = await ArticleController.list(data, true);

  ctx.response.status = 200
  ctx.body = res.json(result)
})

router.post('/unlock', new Auth(ADMIN).m, async ctx => {
  const data = new Validator(ctx, {
    id: 'required|number',
    info: ''
  })

  const flag = await ArticleController.unlock(data);
  if(!flag) return;

  ctx.response.status = 200;
  ctx.body = res.success();
})

router.post('/update/status', new Auth().m, async ctx => {
  const data = new Validator(ctx, {
    id: 'required|number',
    status: 'required|include:0,1'
  })

  await ArticleController.updateStatus(data, ctx);

  ctx.response.status = 200
  ctx.body = res.success()
})

module.exports = router;